// #ifndef MAINWINDOW_H
// #define MAINWINDOW_H

// #include <QMainWindow>

// QT_BEGIN_NAMESPACE
// namespace Ui {
// class MainWindow;
// }
// QT_END_NAMESPACE

// class MainWindow : public QMainWindow
// {
//     Q_OBJECT

// public:
//     MainWindow(QWidget *parent = nullptr);
//     ~MainWindow();

// private:
//     Ui::MainWindow *ui;
// };
// #endif // MAINWINDOW_H


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QLabel>
#include <QFileDialog>
#include <QMessageBox>
#include <QStatusBar>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QProgressDialog>
#include <QElapsedTimer>
#include <QLineEdit>
#include <QGroupBox>
#include <QComboBox>
#include <QCheckBox>
#include <QVector>
#include <QByteArray>
#include <QDateTime>
#include <QTableWidget>
#include <QHeaderView>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QChartView>
#include <QLineSeries>
#include <QtCharts/QValueAxis>
#include <QChart>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

// QT_CHARTS_USE_NAMESPACE
// using namespace QCharts;

// 数据结构定义 - 移到类定义之前
typedef union {
    unsigned char u[4];
    float f;
} UToF;

typedef union {
    char c[2];
    short s;
} UToS;

typedef struct {
    float x;
    float y;
    float z;
} Vector3F;

typedef struct {
    short x;
    short y;
    short z;
} Vector3S;

typedef struct {
    Vector3F accel;
    Vector3F gyro;
    Vector3F euler;  // 欧拉角 (roll, pitch, yaw)
    Vector3S rawAccel;
    Vector3S rawGyro;
    qint64 timestamp;
} SensorData;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void openTextFile();
    void parseData();
    void clearAll();
    void searchFrameHeader();
    void openSerialPort();
    void closeSerialPort();
    void readSerialData();
    void updateChart();

private:
    Ui::MainWindow *ui;

    // 控件
    QTextEdit *logTextEdit;
    QTextEdit *hexDisplayTextEdit;
    QLineEdit *frameHeaderInput;
    QPushButton *openButton;
    QPushButton *parseButton;
    QPushButton *clearButton;
    QPushButton *searchButton;
    QPushButton *openSerialButton;
    QPushButton *closeSerialButton;
    QCheckBox *autoParseCheck;
    QComboBox *serialPortComboBox;
    QComboBox *baudRateComboBox;

    // 图表相关
    QChart *chart;
    QChartView *chartView;
    QLineSeries *rollSeries;
    QLineSeries *pitchSeries;
    QLineSeries *yawSeries;
    QValueAxis *axisX;
    QValueAxis *axisY;

    // 串口
    QSerialPort *serialPort;

    // 数据
    QByteArray fileData;
    QString currentFilePath;
    QVector<SensorData> parsedData;
    QVector<int> framePositions;
    int dataPointCount;

    // 常量
    const QByteArray DEFAULT_FRAME_HEADER = QByteArray::fromHex("4178FF068144");
    const float ACC_GAIN[4] = {0.9765625f, 1.953125f, 3.90625f, 7.8125f};
    const float GYRO_GAIN[4] = {0.0076296273689993f, 0.0152592547379986f,
                                0.0305185094759972f, 0.0610370189519944f};

    void setupUI();
    void setupConnections();
    void updateLog(const QString &message);
    void displayHexData();
    bool parseSensorData(const QByteArray &frameData, SensorData &sensorData);
    Vector3F parseCalibrationData(const QByteArray &data, int offset);
    Vector3S parseRawData(const QByteArray &data, int offset);
    Vector3F parseEulerData(const QByteArray &data, int offset);
    QString vector3FToString(const Vector3F &vec);
    QString vector3SToString(const Vector3S &vec);
    QByteArray hexStringToByteArray(const QString &hexString);
};

#endif // MAINWINDOW_H
